import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/entity/motorcade_entity.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/pages/quota/quota_controller.dart';
import 'package:nyh_motorcade_app/utils/sp_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';

class BatchQuotaController extends GetxController {
  TextEditingController quotaController = TextEditingController();
  FocusNode quotaFocus = FocusNode();
  bool isEditDriver = false;
  String motorcadeId = "";
  String motorcadeName = "";
  String cycleType = "";
  String cycleName = "";
  String unitType = "";
  String unitName = "";
  double quota = 0.0;
  List<MotorcadeEntity> motorcadeList = [];
  List<Cycle> cycleList = [];
  List<Unit> unitList = [];

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  @override
  void onReady() {
    super.onReady();
    isEditDriver = Get.arguments ?? false;
  }

  void setQuota(String value) {
    if (value.isEmpty) {
      quota = 0.0;
    } else {
      quota = double.parse(value);
    }
    update();
  }

  Future<void> onConfirm() async {
    if (motorcadeId.isEmpty || cycleType.isEmpty || cycleType.isEmpty || quota <= 0.0) {
      return;
    }
    quotaFocus.unfocus();
    httpManager.post(
      Get.context,
      isEditDriver ? HttpAddress.BATCH_QUOTA_DRIVER : HttpAddress.BATCH_QUOTA_VEHICLE,
      {
        "superId": await SPUtils.getString(SPUtils.COMPANY_ID),
        "motorcadeId": motorcadeId,
        "circle": cycleType,
        "unit": unitType,
        "totalQuota": quota,
      },
      isToast: true,
      isLoadingDialog: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          return;
        }
        ToastUtils.toast("设置成功");
        Get.back(result: {});
      },
    );
  }

  bool isComplete() {
    return motorcadeId.isNotEmpty && cycleType.isNotEmpty && cycleType.isNotEmpty && quota > 0.0;
  }

  void changeMotorcade(int index) {
    String id = motorcadeList[index].id ?? "";
    if (motorcadeId != id) {
      motorcadeId = id;
      motorcadeName = motorcadeList[index].name ?? "";
      update();
    }
  }

  void changeCycleType(int index) {
    String type = cycleList[index].type;
    if (cycleType != type) {
      cycleType = type;
      cycleName = cycleList[index].name;
      update();
    }
  }

  void changeUnitType(int index) {
    String type = unitList[index].type;
    if (unitType != type) {
      unitType = type;
      unitName = unitList[index].name;
      update();
    }
  }

  void initData() {
    cycleList.add(Cycle("ONCE", "次"));
    cycleList.add(Cycle("DAY", "天"));
    cycleList.add(Cycle("WEEK", "周"));
    cycleList.add(Cycle("MONTH", "月"));
    cycleList.add(Cycle("YEAR", "年"));

    unitList.add(Unit("YUAN", "元"));
    unitList.add(Unit("LITRE", "升"));
  }

  Future<List<MotorcadeEntity>> getMotorcadeList() async {
    if (motorcadeList.isNotEmpty) {
      return motorcadeList;
    }
    HttpResult httpResult = await httpManager.post(Get.context, HttpAddress.GET_MOTORCADE_FILTER_LIST, {"superId": await SPUtils.getString(SPUtils.COMPANY_ID)}, isToast: true, isLoadingDialog: true);
    dynamic data = httpResult.data;
    if (data == null) {
      return [];
    }
    if (data is List) {
      motorcadeList = data.map((element) {
        return MotorcadeEntity().fromJson((element as Map<String, dynamic>));
      }).toList();
      return motorcadeList;
    }
    return [];
  }
}
